using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDanhMucCon()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectDanhMucConByIDDanhMucChinh(int iddanhmucchinh)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucChinh = iddanhmucchinh,
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
			};
			return View("SelectDanhMucCon", CreateViewModel(data));
		}

		public ActionResult SelectDanhMucConByIDDanhMucChinhPaging(int iddanhmucchinh)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucChinh = iddanhmucchinh,
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
			};
			return PartialView("Templates/TH_ListDanhMucCon", CreateViewModel(data));
		}


        public ActionResult DeleteDanhMucCon(int id, int iddanhmucchinh, int page = 1)
		{
			var result = _repDanhMucCon.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
				CurrentPage = page,
                IDDanhMucChinh = iddanhmucchinh
			};
			return View("SelectDanhMucCon", CreateViewModel(data));
		}

        public ActionResult EditDanhMucCon(int page, int id, int iddanhmucchinh)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
				CurrentPage = page,
				IntID = id,
                IDDanhMucChinh = iddanhmucchinh
			};
			return PartialView("Templates/TH_EditDanhMucCon", CreateViewModel(data));
		}

        public ActionResult CancelEditingDanhMucCon(int page, int iddanhmucchinh)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
				CurrentPage = page,
                IDDanhMucChinh = iddanhmucchinh
			};
			return PartialView("Templates/TH_ListDanhMucCon", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDanhMucCon(FormCollection forms)
		{
			string id = forms.GetValues("DanhMucCon_ID")[0];
			string iddanhmucchinh = forms.GetValues("DanhMucCon_IDDanhMucChinh")[0];
			string tendanhmuccon = forms.GetValues("DanhMucCon_TenDanhMucCon")[0];
			var danhmuccon = _repDanhMucCon.SelectByID(int.Parse(id));
			danhmuccon.IDDanhMucChinh = int.Parse(iddanhmucchinh.Replace(".", "").Replace(",", ""));
			danhmuccon.TenDanhMucCon = tendanhmuccon;
			var result = _repDanhMucCon.Update(danhmuccon);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
                IDDanhMucChinh = int.Parse(iddanhmucchinh)
			};
			return View("SelectDanhMucCon", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDanhMucCon(FormCollection forms)
		{
			string iddanhmucchinh = forms.GetValues("DanhMucCon_IDDanhMucChinh")[0];
			string tendanhmuccon = forms.GetValues("DanhMucCon_TenDanhMucCon")[0];
			var newItem = new DanhMucConDTO
			{
				IDDanhMucChinh = int.Parse(iddanhmucchinh),
				TenDanhMucCon = tendanhmuccon
			};
			var result = _repDanhMucCon.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDDanhMucChinh = int.Parse(iddanhmucchinh),
				EnumViewModelType = EnumViewModel.ADMIN_DANHMUCCON,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectDanhMucCon", CreateViewModel(data));
		}


	}
}